
module.exports = function combineReducers(reducers) {
    var reducerKeys = Object.keys(reducers)
    var finalReducers = {}
    reducerKeys.forEach((key,i)=>{
        if (typeof reducers[key] === 'function') {
            finalReducers[key] = reducers[key]
        }
    })
    var finalReducerKeys = Object.keys(finalReducers)

    //返回结合起来的 Reducer
    return function combination(state = {}, action) {
        var hasChanged = false
        var nextState = {}
        for (var i = 0; i < finalReducerKeys.length; i++) {
            var key = finalReducerKeys[i]
            var reducer = finalReducers[key]
            var previousStateForKey = state[key]
            var nextStateForKey = reducer(previousStateForKey, action)
            nextState[key] = nextStateForKey
            hasChanged = hasChanged || nextStateForKey !== previousStateForKey
        }
        // 判断分片 state 是否变化
        hasChanged = hasChanged || finalReducerKeys.length !== Object.keys(state).length
        return hasChanged ? nextState : state
    }
}